package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//批量插入操作
public class Demo01Batch {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://125.74.233.192:3306/fruitdb?characterEncoding=utf-8&rewriteBatchedStatements=true&useSSL=false";
        String user = "root";
        String password = "123456123";
//        long l = System.currentTimeMillis();
        Connection connection = DriverManager.getConnection(url, user, password);
//        long l1 = System.currentTimeMillis();


        String sql = "insert into t_fruit values(0,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        for (int i = 0; i <10; i++) {
            statement.setString(1,"西瓜"+i);
            statement.setInt(2,15);
            statement.setInt(3,18);
            statement.setString(4,"非常hao");
            // 添加批处理操作入队
            statement.addBatch();

        }
        // 统一做处理
        int[] ints = statement.executeBatch();
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);

        }
        connection.close();




    }
}
